<?php
class PostsController extends AppController{
	var $name = 'Posts';
	var $uses = array('Category', 'Post');
	var $components = array('Session', 'ImageUpload', 'Auth', 'Filter', 'RequestHandler');
	var $helpers = array('Js' => array('Jquery'));
	var $paginate = array(
        'limit' => 5,
        'order' => array(
            'Post.created' => 'desc'
        )
    );
	function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('view', 'home');
	}
	function index(){
		$filter = $this->Filter->process($this);
    	//$this->set('posts', $this->Post->find('all'));
    	$this->set('list_category', $this->Category->find('list'));
    	$this->set('posts', $this->paginate('Post', $filter));
    	$this->set('isFilterPost', $filter);
    }
	function add() {
		//KCFinder
		//$this->Session->write('KCFINDER.disabled',false);
		
		$this->Post->create();
		$this->set('list_category', $this->Category->find('list'));
		if (!empty($this->data))
		{
			if($this->ImageUpload->uploadCrop ($this->data['Post']['image_file'], WWW_ROOT . 'img/news' . DS))
			{
				$this->data['Post']['image'] = $this->data['Post']['image_file']['name'];
			}
			else {
				$this->data['Post']['image'] = 'image_default_news.jpg';
			}
			if($this->ImageUpload->upload ($this->data['Post']['hotimg'], WWW_ROOT . 'img/news' . DS))
			{
				$this->data['Post']['hot_img'] = $this->data['Post']['hotimg']['name'];
			}
			if ($this->Post->save($this->data)) {
				$this->flash(__('Thêm bài viết mới thành công', true), array('action' => 'index'));
			}
			else{
				$this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('action' => 'index'));
			}
		}
	}
	function edit($id = null) {
		$this->Post->id = $id;
		$this->set('list_category', $this->Category->find('list'));
		if (empty($this->data)) {
			
			$this->data = $this->Post->read();
			
		} else {
			if($this->ImageUpload->uploadCrop ($this->data['Post']['image_file'], WWW_ROOT . 'img/news' . DS))
			{
				$this->data['Post']['image'] = $this->data['Post']['image_file']['name'];
				//Xóa hình cu
				$image = $this->Post->read('image', $id);
				unlink(WWW_ROOT . 'img/news/thumb_' . $image['Post']['image']);
			}
			if($this->ImageUpload->upload ($this->data['Post']['hotimg'], WWW_ROOT . 'img/news' . DS))
			{
				$this->data['Post']['hot_img'] = $this->data['Post']['hotimg']['name'];
			}
			if ($this->Post->save($this->data)) {
				$this->flash(__('Sửa bài viết thành công', true), array('action' => 'index'));
			}
			else
				$this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('action' => 'index'));
		}
	}
	function delete($id) {
		$this->Post->delete($id);
		$this->redirect(array('action' => 'index'));
	}
	function delMulti(){
		if($this->data['SelectedPosts'])
		{
			$listSelect = array();
			foreach($this->data['SelectedPosts'] as $key => $value) {
				if($value != '0') {
					array_push($listSelect, $value);
					
					//$this->UnionMember->delete($value);
					//$this->flash(__($value, true), array('action' => 'index'));
		 		}
			}
			$this->set('posts', $this->Post->find('all', array('conditions'=>array('Post.id'=>$listSelect))));
			$this->Session->write('listDel', $listSelect);
		}
		else{
			if($this->Session->check('listDel')){
				foreach($this->Session->read('listDel') as $value){
					$this->Post->delete($value);
					
				}
				$this->Session->delete('listDel');
				$this->flash(__('Đã xóa thành công', true), array('action' => 'index'));
			}
			else{
				$this->flash(__('Thao tác thất bại, vui lòng thử lại', true), array('action' => 'index'));
			}
		}
	}
	function view($id){
		$this->layout = "news";
		$this->Post->id = $id;
		$post = $this->Post->read();
		if($post['Post']['active'] != 0)
			$this->set('post', $post);
	}
	function home(){
		$this->layout = "news";
		$list_cat = $this->Category->find('all', array(
				'fields'=>array('Category.id', 'Category.category'),
				'conditions' => array('Category.active !=' => 0)
		));
		$posts = array();
		foreach($list_cat as $cat){
			$listpost = $this->Post->find('all', array(
				'conditions' => array('Post.category_id' => $cat['Category']['id'], 'Post.active !='=>0),
				'fields' => array('Post.id', 'Post.title', 'Post.summary', 'Post.image'),
				'order' => array('Post.created desc'),
				'limit' => 5
			));
			$posts[] = array('cat_id'=>$cat['Category']['id'],'category'=>$cat['Category']['category'], 'listpost'=>$listpost);
		}
		$this->set('posts', $posts);
		
		//Hot news
		$cat_active = array();
		foreach($list_cat as $cat){
			$cat_active[] = $cat['Category']['id'];
		}
		$hot_news = $this->Post->find('all', array(
			'conditions' => array('Post.category_id' => $cat_active, 'Post.active >'=>1),
				'fields' => array('Post.id', 'Post.title', 'Post.created', 'Post.summary', 'Post.hot_img', 'Post.category_id', 'Category.category'),
				'order' => array('Post.created desc'),
				'limit' => 5
		));
		$this->set('hot_news', $hot_news);
		
	}
	function cat($id){
		$this->layout = "news";
		$cat = $this->Category->find('first', array(
				'conditions' => array('Category.id' => $id)
		));
		if ($cat['Category']['active'] > 0){
			$posts = $this->Post->find('all', array(
			'conditions' => array('Post.category_id' => $id, 'Post.active >'=>0),
				'fields' => array('Post.id', 'Post.title', 'Post.created', 'Post.summary', 'Post.image', 'Category.category'),
				'order' => array('Post.created desc'),
				'limit' => 5
			));
			$this->set('posts', $posts);
			$this->set('category', $cat['Category']['category']);
		}
	}
}
?>